var myiscroll2;
$(document).ready( function() {
	$(window).resize(function(){//当窗口大小改变时自动重新计算
		iscroll();
		myiscroll2.refresh();
	});
	var report_date="";
	var mm;
	report_date = getQueryParam("report_date");
	if(report_date!=""){
		mm = report_date.substr(5);
	}else{
		var date = new Date();
		mm = date.getMonth()+1;
		var day = date.getDate();
		var year=date.getFullYear();
		if(day<10){
			if(mm==1){
				mm=12;
				year = year-1;
			}else
				mm=mm-1;
		}
		if(mm<10)
			mm="0"+mm;
		report_date = year+"/"+mm;
//		var today = new Date();
//			mm = today.getMonth() + 1;
//		var yy = today.getFullYear();
//		if (mm < 10)
//			mm = "0" + mm;
//		report_date = yy + "/" + mm;
	}
	var te ='下属' + mm + '月汇报列表';
	$("#report_date").html(te);
	//获得时间
	$('#mydate').scroller('destroy').scroller({
		preset: 'date',
		dateFormat: 'yymm',
		dateOrder: 'yymm',
		minDate: new Date(2002, 3, 10, 9, 22),
		maxDate: new Date(2030, 7, 30, 15, 44),
		theme: 'ios',
		mode: 'scroller',
		lang: 'zh',
		display: 'bottom',
		animate: 'slideup'
	});
	$("#fList").empty();
	$("#sList").empty();
	$("#loadingText").show();
	//查询下属
	hbquery(report_date);
	//返回主页
	$("#back").on("tap",function(){
		//location.href="/mobile/index.html";
		gotoLocation("/mobile/index.html","");
	});
});
//显示时间插件
function show(){
	$("#mydate").scroller('show');
	$(".dwo").css("background-color","#000");
    $(".dwo").css("opacity",".5");
    $(".dwo").css("z-index","1000");
}
$(document).on("tap", function(){
    $(".dw dwbg").on("tap",function(){
    	return;
    })
    $(".dwo").on("tap",function(){
    	$("#promptBox").remove("slow");	
    })
    $(".dwbw dwb-c").on("tap",function(){
    	$("#promptBox").remove("slow");	
    })
    $(".dwbw dwb-s").on("tap",function(){
    	$("#promptBox").remove("slow");	
    })
});
//页面滚动
function iscroll(){ 
	var wh = $(window).height()-90-122; 
    if(myiscroll2 instanceof iScroll){
    	myiscroll2.destroy();
    }
    $("#content").css("height",wh+"px");    
    myiscroll2 = new iScroll("content",{bounce:false});
}
// 更改时间，并查询
function change() {
	var da = $("#mydate").val();
	var yy = da.substring(0, 4);
	var mm = da.substring(4, da.length);
	var report_date = yy + "/" + mm;
	var te ='下属' + mm + '月汇报列表';
	$("#report_date").html(te);
	//location.href="/mobile/underlingReport/xslb.html?report_date="+report_date;
	var param = "report_date="+ report_date;
	gotoLocation("/mobile/underlingReport/xslb.html",param);
}
//查询数据
function hbquery(report_date) {
	$("#errorMask").hide();
	$.ajax({
		type : "post",
		url : mpsurl+"/mobile/report/queryUnderlingReport.do",
		dataType : "json",
		
		data : {
			method : "xslbwork",
			report_date : report_date
		},
		success : function(data) {
			$("#loadingText").hide();
			data = eval(data);
			if (data.resultCode == "500600") {
				ReportData(data, "#fList", "#sList");
			} else {
				var tourl="/mobile/underlingReport/xslb.html?report_date="+report_date;
				checkLogin(data.resultCode,tourl,data.resultMessage);
			}
		},
		error : function(XMLHttpRequest, textStatus, errorThrown) {
			$("#loadingText").hide();
			errorMask("网络连接异常");
		}
	});
}
//结果处理
function ReportData(data, id, id1) {
	var value = data.value;
	value = eval(value);
	var my_emplid = data.emplid;
	var cls = "";//样式
	var status = {
		SUBM:"待确认",
		APP:"已确认",
		USUB:"未提交",
		NON:"未提交",
		BAC:"已驳回",
		" ":"未提交"
	};
	var va = value;
	var obj=[];
	var r =0;
	for ( var i = 0; i < value.length; i++) {
		if(value[i].report_status == "SUBM"){
			obj[r] = value[i];
			r++;
		}
	}
	for ( var i = 0; i < value.length; i++) {
		if(value[i].report_status == "APP"){
			obj[r] = value[i];
			r++;
		}
	}
	for ( var i = 0; i < value.length; i++) {
		if(value[i].report_status == "USUB"){
			obj[r] = value[i];
			r++;
		}
	}
	for ( var i = 0; i < value.length; i++) {
		if(value[i].report_status == "NON"){
			obj[r] = value[i];
			r++;
		}
	}
	for ( var i = 0; i < value.length; i++) {
		if(value[i].report_status == "BAC"){
			obj[r] = value[i];
			r++;
		}
	}
	value = obj;
	for ( var i = 0; i < value.length; i++) {
		var url = "";
		var param="";
		var underling_name = value[i].underling_name;//下属名字
		var report_status = value[i].report_status;//评审状态
		var mark = value[i].mark;//审批人标记
		var report_date = value[i].report_date;
		var emplid = value[i].underling_emplid;//下属id
		var ep_manager2_status = value[i].ep_manager2_status;
		var ep_manager3_status = value[i].ep_manager3_status;
		var ep_manager2_id = value[i].ep_manager2_id;
		if(report_status == "SUBM"){
			cls = "state-unsure";
		}else if(report_status == "APP"){
			cls = "state-sure";
		}else{
			cls = "state-unsubmit";
		}
		if(mark == 1){
			if(report_status == "SUBM"){
				//url = "xslb-chakan.html?emplid="+ emplid + "&report_date="+report_date+"&name="+underling_name;
				url = "/mobile/underlingReport/xslb-chakan.html";
				param ="emplid="+ emplid + "&report_date="+report_date+"&name="+underling_name;
			}else if(report_status == "APP"){
				//url = "xslb-chakan2.html?emplid="+ emplid + "&report_date="+report_date+"&name="+underling_name;
				url = "/mobile/underlingReport/xslb-chakan2.html";
				param ="emplid="+ emplid + "&report_date="+report_date+"&name="+underling_name;
			}
		}else if( mark == 2){
			if(my_emplid==value[i].ep_manager2_id){
				if(ep_manager2_status!="Y"){
					//url = "xslb-chakan.html?emplid="+ emplid + "&report_date="+report_date+"&name="+underling_name;
					url = "/mobile/underlingReport/xslb-chakan.html";
					param ="emplid="+ emplid + "&report_date="+report_date+"&name="+underling_name;
				}else {
					//url = "xslb-chakan2.html?emplid="+ emplid + "&report_date="+report_date+"&name="+underling_name;
					url = "/mobile/underlingReport/xslb-chakan2.html";
					param ="emplid="+ emplid + "&report_date="+report_date+"&name="+underling_name;
				}
			}else if(my_emplid==value[i].ep_manager3_id){
				if(ep_manager3_status!="Y"){
					//url = "xslb-chakan.html?emplid="+ emplid + "&report_date="+report_date+"&name="+underling_name;
					url = "/mobile/underlingReport/xslb-chakan.html";
					param ="emplid="+ emplid + "&report_date="+report_date+"&name="+underling_name;
				} else {
					//url = "xslb-chakan2.html?emplid="+ emplid + "&report_date="+report_date+"&name="+underling_name;
					url = "/mobile/underlingReport/xslb-chakan2.html";
					param ="emplid="+ emplid + "&report_date="+report_date+"&name="+underling_name;
				}
			}
		}
		/*
		param ="emplid="+ emplid + "&report_date="+report_date+"&name="+underling_name;
		if((mark == 1&&report_status == "SUBM")
			|| (mark == 2&&my_emplid==value[i].ep_manager2_id && ep_manager2_status!="Y")
			|| (mark == 2&&my_emplid==value[i].ep_manager3_id && ep_manager3_status!="Y")){
			var url1 = "/mobile/underlingReport/xslb-chakan.html";
		}else{
			var url1 = "/mobile/underlingReport/xslb-chakan2.html";
		}
		*/
		// 解析评审状态
		if(report_status == "SUBM"||report_status == "APP"){
//		if(report_status == "SUBM"){
			//var li = $("<li class="+ cls + " onclick=\""+gotoLocation(url,param)+"\">" 
			var li = $("<li class=\""+ cls + "\">" 
					+"<input type=\"hidden\" value=\""+url + "?"+param+"\"/>"
					+ "<div class=\"info-list-cont\">"+ underling_name + "</div>"
					+ "<div class=\"info-list-state\">" + status[report_status] + "</div>"
					+ "<div class=\"info-list-icon next-icon\"></div>"
					+ "</li>");
			li.on("tap",function(){
		        var param1 = $(this).find("input[type=hidden]").val();
		        var url2 = param1.split("?")[0];
		        var param2 = param1.split("?")[1];
		        gotoLocation(url2,param2);
		    });
//		}else if(report_status == "APP"){
//			var li = $("<li class=\""+ cls + "\">" 
//					+"<input type=\"hidden\" value=\""+param+"\"/>"
//					+ "<div class=\"info-list-cont\">"+ underling_name + "</div>"
//					+ "<div class=\"info-list-state\">" + status[report_status] + "</div>"
//					+ "<div class=\"info-list-icon next-icon\"></div>"
//					+ "</li>");
//			li.on("tap",function(){
//		        var param1 = $(this).find("input[type=hidden]").val();
//		        errorMask(url1+"==="+param1);
//		        gotoLocation(url1,param1);
//		    });
		}else{
			var li = $("<li class="+cls+">" 
					+ "<div class=\"info-list-cont\">"+ underling_name + "</div>"
					+ "<div class=\"info-list-state\">" + status[report_status] + "</div>"
					+ "<div class=\"info-list-icon\"></div>"
					+ "</li>");
		}
		//根据评审权限分别添加在不同的块中
		if (mark == 1) {
			$(id).append(li);
		} else {
			$(id1).append(li);
		}
	}
	iscroll();//设置滚动
	//没有下属列表时显示的消息
	var msg = "<div class=\"tabs-content\" id=\"J-TabsContent\">"
			+ "<div class=\"no-record-prompt\">"
			+ "<div class=\"prompt-icon-txt\">" + "暂无下属月度汇报" + "</div>"
			+ "</div></div>";
	if ($(id).children().length == 0) {
		$(id).append(msg);
	}
	if ($(id1).children().length == 0) {
		$(id1).append(msg);
	}
}

